/*
 * BuscarCliente.java
 *
 * Created on __DATE__, __TIME__
 */

package cl.pelu.operacion;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import cl.pelu.main.PeluqueriaApp;
import cl.pelu.model.bo.dvo.ClienteDVO;
import cl.pelu.model.bo.dvo.ServicioDetalleBO;
import cl.pelu.model.bo.dvo.TablaDVO;
import cl.pelu.model.bo.dvo.TipoCabelloDVO;
import cl.pelu.utils.PeluFacade;

/**
 *
 * @author  __USER__
 */
public class BuscarCliente extends  javax.swing.JDialog {

	private static BuscarCliente myInstance;

	
	private javax.swing.JButton buscarButton;
	private javax.swing.JLayeredPane busquedaClienteLayeredPane;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable clienteTable;
	private javax.swing.JLabel busquedaClienteLabel;
	private javax.swing.JTextField busquedaClienteField;
	
	public String getRut() {
		return rut;
	}

	public void setRut(String rut) {
		this.rut = rut;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	private String rut;
	private String nombre;
	
	
	public BuscarCliente(java.awt.Frame parent, boolean modal) throws SQLException {
		super(parent, modal);
		initComponents();
		
	}
	
	public BuscarCliente() {
		initComponents();
	}
	
	
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		clienteTable = new javax.swing.JTable();
		busquedaClienteLayeredPane = new javax.swing.JLayeredPane();
		busquedaClienteLabel = new javax.swing.JLabel("Busqueda (Nombre o Rut)");
		busquedaClienteField = new javax.swing.JTextField();
		buscarButton = new javax.swing.JButton("Buscar");
		rut = new String();
		nombre = new String ();
		
		
		clienteTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "Rut", "Nombre", "Direccion", "Felefono","Correo" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		
		
		clienteTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				try {
					jTable1MouseClicked(evt);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		jScrollPane1.setViewportView(clienteTable);

		busquedaClienteLayeredPane.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(),	"Busqueda Clientes"));


		
		
		busquedaClienteLabel.setBounds(30, 30, 200, 20);
		busquedaClienteField.setBounds(240, 30, 350	, 20);
		buscarButton.setBounds(600, 30, 100, 20);

		
		
		busquedaClienteLayeredPane.add(busquedaClienteLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaClienteLayeredPane.add(busquedaClienteField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaClienteLayeredPane.add(buscarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		
		
		buscarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
			
				ClienteDVO cliente = new ClienteDVO();
				
				if(!busquedaClienteField.getText().isEmpty()){
					cliente.setNombre(busquedaClienteField.getText());
					cliente.setRut(busquedaClienteField.getText());
				}
				
				
				
				limpiarTabla();
				cargarTabla(cliente);
				
			}
		});
		
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(49, 49, 49)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														800,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														busquedaClienteLayeredPane,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														800,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(134, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGap(17, 17, 17)
						.addComponent(busquedaClienteLayeredPane,
								javax.swing.GroupLayout.PREFERRED_SIZE, 149,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(27, 27, 27)
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 148,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(99, Short.MAX_VALUE)));

		pack();
	}
	
	
	private void jTable1MouseClicked(java.awt.event.MouseEvent evt) throws SQLException 
	{

		this.rut = (String )this.clienteTable.getValueAt(this.clienteTable.getSelectedRow(), 0);
		this.nombre = (String)this.clienteTable.getValueAt(this.clienteTable.getSelectedRow(), 1);
		dispose();
		
	}

	

	public static BuscarCliente getInstance(java.awt.Frame parent, boolean modal) throws PropertyVetoException, SQLException 
	{
		if (myInstance == null) {
			myInstance = new BuscarCliente(parent,modal);
		}
		return myInstance;
	}
	
	void limpiarTabla (){
		
		DefaultTableModel tableModel = (DefaultTableModel) this.clienteTable.getModel();
		
		int a = tableModel.getRowCount();
		
		for (int i = 0; i < a; i++) {
			tableModel.removeRow(0);
			
		}
		
	}
	
	private void cargarTabla(ClienteDVO cliente) 
	{
		

		
		try {
				ArrayList <ClienteDVO>  clientesDVO= PeluFacade.getPeluqueriaComponent().getClientes(cliente);
				DefaultTableModel tableModel = (DefaultTableModel) this.clienteTable.getModel();
				for (ClienteDVO clienteDVO : clientesDVO) {
				
						
								tableModel.addRow(new Object[] { 
										clienteDVO .getRut(),
										clienteDVO .getNombre(),
										clienteDVO .getDireccion(),
										clienteDVO .getTelefono(),
										clienteDVO .getCorreo()
									});
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void limpiarFormulario(){
			this.limpiarTabla();
			
	}

}